﻿using MT.CustomWindows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MT.CustomUserControl.Views.Windows
{
    /// <summary>
    /// LoadingWindow2.xaml 的交互逻辑
    /// </summary>
    public partial class LoadingWindow2 : Window
    {
        /// <summary>
        /// 提示文字
        /// </summary>
        public static DependencyProperty HintTextProperty = DependencyProperty.Register("HintText", typeof(string), typeof(LoadingWindow2), new FrameworkPropertyMetadata("正在执行，请稍后...", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
        public string HintText
        {
            get { return (string)GetValue(HintTextProperty); }
            set { SetValue(HintTextProperty, value); }
        }

        private Action LoadAction;

        public LoadingWindow2()
        {
            InitializeComponent();
        }

        private LoadingWindow2(Action act, string mes)
        {
            InitializeComponent();

            LoadAction = act;
            HintText = mes;
        }


        private void WinLoading_Loaded(object sender, RoutedEventArgs e)
        {

        }

        public static void Loading(Action act, string mes = "请稍后...")
        {
            LoadingWindow2 win = new LoadingWindow2(act, mes);
            var task = Task.Factory.StartNew(() => { win.LoadAction?.Invoke(); });
            Task.Factory.StartNew(() =>
            {
                Task.WaitAll(task);
                win.Dispatcher.Invoke(new Action(() =>
                {
                    win.Close();
                }));
            });
            win.ShowDialog();
        }
    }
}
